<?php

/**
 * Process.
 *
 * @author  yyc
 * @version 1.0
 * @package main
 * @date 2017-06-08 0008
 * @time 11:51:54
 */

/**
 *
 * ━━━━━━神兽出没━━━━━━
 * 　　　┏┓　　　┏┓
 * 　　┏┛┻━━━┛┻┓
 * 　　┃　　　　　　　┃
 * 　　┃　　　━　　　┃
 * 　　┃　┳┛　┗┳　┃
 * 　　┃　　　　　　　┃
 * 　　┃　　　┻　　　┃
 * 　　┃　　　　　　　┃
 * 　　┗━┓　　　┏━┛Code is far away from bug with the animal protecting
 * 　　　　┃　　　┃    神兽保佑,代码无bug
 * 　　　　┃　　　┃
 * 　　　　┃　　　┗━━━┓
 * 　　　　┃　　　　　　　┣┓
 * 　　　　┃　　　　　　　┏┛
 * 　　　　┗┓┓┏━┳┓┏┛
 * 　　　　　┃┫┫　┃┫┫
 * 　　　　　┗┻┛　┗┻┛
 *
 * ━━━━━━感觉萌萌哒━━━━━━
 */

namespace server;


class Process
{
    private $pidDir = '';
    private $pids = [];
    private $works = [];
    private $nochdir = true;
    private $noclose = true;
    public function __construct()
    {

        $this->pidDir = __DIR__.'/../../pid/';
        if(!is_dir( $this->pidDir)){
            mkdir( $this->pidDir,0777,true);
        }

    }

    private function creatPid($pid = ''){
        //$dir = realpath(__DIR__.'/../../../pid/');
        $dir = $this->pidDir;
        file_put_contents($dir.'/SMSGateWay.pid',implode(',',$pid));
    }

    private function putPid($pid){
        if($pid){
            file_put_contents($this->pidDir.'/SMSGateWay.pid',','.$pid,FILE_APPEND);
        }
    }

    public function run(){
        self::starClient();
        self::startCancel();
        self::startDeliver();
        self::startQuery();
        self::startSubmit();
        self::startTermin();

        //self::creatPid($this->pids);
    }



    public function starClient(){
        $process = new \swoole_process(function($worker){

            $worker->name('SMS-Client');
            $worker->daemon($this->nochdir,$this->noclose);
            $this->creatPid([getmypid()]);
            $cli = new \server\Client();
            $cli->run();


        });
        $pid = $process->start();
        if($pid === false){
            L()->error('启动客户端进程失败',[swoole_strerror(swoole_errno())]);
        }else{
            $this->works[$pid] = $process;
        }
    }

    public function startCancel(){
        $process = new \swoole_process(function($worker){
            $worker->name('SMS-Cancel');
            $worker->daemon($this->nochdir,$this->noclose);
            $this->putPid(getmypid());
            $obj = new \app\Cancel();
            $obj->run();
        });
        $pid = $process->start();
        if($pid === false){
            L()->error('启动短信删除进程失败',[swoole_strerror(swoole_errno())]);
        }else{
            $this->works[$pid] = $process;
        }
    }

    public function startDeliver(){
        $process = new \swoole_process(function($worker){
            $worker->name('SMS-Deliver');
            $worker->daemon($this->nochdir,$this->noclose);
            $this->putPid(getmypid());
            $obj = new \app\Deliver();
            $obj->run();
        });
        $pid = $process->start();
        if($pid === false){
            L()->error('启动deliver进程失败',[swoole_strerror(swoole_errno())]);
        }else{
            $this->works[$pid] = $process;
        }

        $process = new \swoole_process(function($worker){
            $worker->name('SMS-UP');
            $worker->daemon($this->nochdir,$this->noclose);
            $this->putPid(getmypid());
            $obj = new \app\Deliver();
            $obj->up();
        });
        $pid = $process->start();
        if($pid === false){
            L()->error('启动短信上行进程失败',[swoole_strerror(swoole_errno())]);
        }else{
            $this->works[$pid] = $process;
        }
    }

    public function startQuery(){
        $process = new \swoole_process(function($worker){
            $worker->name('SMS-Query');
            $worker->daemon($this->nochdir,$this->noclose);
            $this->putPid(getmypid());
            $obj = new \app\Query();
            $obj->run();
        });
        $pid = $process->start();
        if($pid === false){
            L()->error('启动短信查询状态进程失败',[swoole_strerror(swoole_errno())]);
        }else{
            $this->works[$pid] = $process;
        }
    }
    public function startSubmit(){
        $process = new \swoole_process(function($worker){
            $worker->name('SMS-Submit');
            $worker->daemon($this->nochdir,$this->noclose);
            $this->putPid(getmypid());
            $obj = new \app\Submit();
            $obj->run();
        });
        $pid = $process->start();
        if($pid === false){
            L()->error('启动Submit进程失败',[swoole_strerror(swoole_errno())]);
        }else{
            $this->works[$pid] = $process;
        }
    }

    public function startTermin(){
        $process = new \swoole_process(function($worker){
            $worker->name('SMS-Terminate');
            $worker->daemon($this->nochdir,$this->noclose);
            $this->putPid(getmypid());
            $obj = new \app\Terminate();
            $obj->run();
        });
        $pid = $process->start();
        if($pid === false){
            L()->error('启动Terminate进程失败',[swoole_strerror(swoole_errno())]);
        }else{
            $this->works[$pid] = $process;
        }
    }

    public function stop(){
        $file = $this->pidDir.'/SMSGateWay.pid';
        if(is_file($file)){
            $pids = file_get_contents($file);
            if($pids){
                $pids = explode(',',$pids);
                if(is_array($pids)){
                    //$pids = array_unique($pids);
                    foreach ($pids as $pid){
                        if(!$pid){
                            continue;
                        }
                        posix_kill($pid, 9);
                    }
                }

            }
            unlink($file);
        }else{
            exit('kill process failure'.PHP_EOL);
        }
    }


    public function stopB(){
        $file = $this->pidDir.'/SMSGateWay.pid';
       // L()->info($file);
        if(is_file($file)){
            $pids = file_get_contents($file);
            if($pids){
                $pids = explode(',',$pids);
                //L()->info('pid',$pids);
                if(is_array($pids)){
                    //$pids = array_unique($pids);
                    foreach ($pids as $pid){
                        if(!$pid){
                            continue;
                        }
                        posix_kill($pid, 9);
                    }
                }

            }
            @unlink($file);
        }else{
            throw new \Exception('kill process failure'.PHP_EOL);

        }
    }



}